package cn.kgc.car.service;

import cn.kgc.car.entity.User;


import java.io.*;
import java.util.List;

/**
 * 序列化
 */
public class UserOpera {
    public static void Seria(List<User> list, File file){
        ObjectOutputStream ost=null;
        if (file.exists()){
            try {
                ost=new ObjectOutputStream(new FileOutputStream(file));
                ost.writeObject(list);
                ost.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (ost!=null){
                    try {
                        ost.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 反序列化
     * @param file
     * @return
     */
    public static List<User> unSeria(File file){
        ObjectInputStream ois = null;
        List<User> list=null;
        try {
            if (file.length() > 0) {
                ois=new ObjectInputStream(new FileInputStream(file));
                list= (List<User>) ois.readObject();
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
